package com.sugon.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.*;
import com.sugon.entity.*;
import com.sugon.service.SysProcessInfoService;
import com.sugon.service.ToolsFileService;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工装档案资料Service实现类
 *
 * @author sugon
 * @date 2019-08-05 09:24:58
 */
@Service("toolsFileService")
public class ToolsFileServiceImpl implements ToolsFileService {
    @Autowired
    private ToolsFileDao toolsFileDao;
    @Autowired
    private AnnexFileDao annexFileDao;
    @Autowired
    private KnowBaseDao knowBaseDao;
    @Autowired
    private ToolsListDao toolsListDao;
    @Autowired
    private KnowFileDao knowFileDao;
    @Autowired
    private SysProcessInfoService sysProcessInfoService;

    /**
     * @param id 主键
     * @return
     */
    @Override
    public ToolsFileEntity queryObject(String id) {
        return toolsFileDao.queryObject(id);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public List<ToolsFileEntity> queryList(Map<String, Object> map) {
        return toolsFileDao.queryList(map);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return toolsFileDao.queryTotal(map);
    }

    /**
     * @param toolsFile 实体
     * @return
     */
    @Override
    public int save(ToolsFileEntity toolsFile) {
        toolsFile.setId(RandomUtil.randomString(32));
        toolsFile.setCreateUser(ShiroUtils.getUserId());
        toolsFile.setCreateDate(new Date());
        toolsFile.setDelFlag(1);
        toolsFile.setKnowBase(0);
        toolsFile.setDeptApprovalStatus(0);
        toolsFile.setLeaderApprovalStatus(0);

        int seq = toolsFileDao.querySeq();
        toolsFile.setUnNo(String.valueOf(seq));
        if (toolsFile.getType() == 2) {
            String nu = toolsFile.getToolsMapNo() + "-" + toolsFile.getToolsEdNo() + "-" + seq;
            toolsFile.setUnNo(nu);
        }
//        sysProcessInfoService.saveProcessInfo(toolsFile.getId(), ProcessInfoEnum.TLS_GZDA, 0, null);
        return toolsFileDao.save(toolsFile);
    }

    /**
     * @param toolsFile 实体
     * @return
     */
    @Override
    public int update(ToolsFileEntity toolsFile) {
        return toolsFileDao.update(toolsFile);
    }

    /**
     * @param toolsFile
     * @return
     */
    @Override
    public int deptCheck(ToolsFileEntity toolsFile) {
        toolsFile.setDeptApproval(ShiroUtils.getUserId());
        toolsFile.setDeptApprovalDate(new Date());
        sysProcessInfoService.saveProcessInfo(toolsFile.getId(), ProcessInfoEnum.TLS_GZDA, toolsFile.getDeptApprovalStatus(), null);
        return toolsFileDao.update(toolsFile);
    }

    /**
     * @param toolsFile
     * @return
     */
    @Override
    public int leaderCheck(ToolsFileEntity toolsFile) {
        toolsFile.setLeaderApproval(ShiroUtils.getUserId());
        toolsFile.setLeaderApprovalDate(new Date());
        sysProcessInfoService.saveProcessInfo(toolsFile.getId(), ProcessInfoEnum.TLS_GZDA, toolsFile.getLeaderApprovalStatus(), null);
        return toolsFileDao.update(toolsFile);
    }

    /**
     * @param toolsFile
     * @return
     */
    @Override
    public int addKnowBase(ToolsFileEntity toolsFile) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("equId", toolsFile.getId());
        //所有的附件信息
        List<AnnexFileEntity> list = annexFileDao.queryToolsEquList(map);
        ToolsFileEntity toolsFileEntity = toolsFileDao.queryObject(toolsFile.getId());
        ToolsListEntity toolslist = toolsListDao.queryObject(toolsFileEntity.getToolsId());
        for (AnnexFileEntity file : list) {
            String baseId = RandomUtil.randomString(32);
            KnowBaseEntity knowBase = new KnowBaseEntity(); //加入知识库
            knowBase.setDevCode(toolslist.getId());
            knowBase.setDevName(toolslist.getName());
            knowBase.setId(baseId);
            knowBase.setType("3");
            knowBase.setIsDel(1);
            knowBaseDao.save(knowBase);
            KnowFileEntity knowFile = new KnowFileEntity(); //知识库关联表
            knowFile.setFileId(file.getId());
            knowFile.setKnowId(baseId);
            knowFile.setType("3");
            knowFile.setId(RandomUtil.randomString(32));
            knowFileDao.save(knowFile);
        }
        //纳入知识库
        toolsFile.setKnowBase(1);
        toolsFile.setUpdateUser(ShiroUtils.getUserId());
        toolsFile.setUpdateDate(new Date());
        return toolsFileDao.update(toolsFile);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return toolsFileDao.delete(id);
    }

    /**
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return toolsFileDao.deleteBatch(ids);
    }

}
